import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-09-28
 * Time: 23:48
 */
//数组中着字符串的最小距离：

public class MinimumDistanceNum {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());

        String[] str = reader.readLine().split(" ");
        String s1 = str[0],s2 = str[1];

        int prev1 = -1;//记录s1位置
        int prev2 = -1;//记录s2位置
        int ret = Integer.MAX_VALUE;

        for(int i = 0; i < n; i++) {

            if(str[i].equals(s1)){ //前面找s2

                if(prev2 != -1){

                    //看左边是否有s2
                    ret = Math.min(ret,i-prev2);
                    prev1 = i;
                }
            }

            if(str[i].equals(s2)){ //前面找s1

                if(prev1 != -1){

                    //看左边是否有s1
                    ret = Math.min(ret,i-prev1);
                    prev2 = i;
                }
            }
        }

        System.out.println(ret == Integer.MAX_VALUE ? -1 : ret);
    }
}
